package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.contract.bean.ProductDetailsBean;

import java.util.ArrayList;

import contract.wwwarehouse.com.contract.R;


public class BuyGoodDetialAdapter extends BaseAdapter {

    private ArrayList<ProductDetailsBean.ListBean> mData;
    private Context mContext;
    private ViewHolder mHolder;
    private ListView mListView;

    public BuyGoodDetialAdapter(ArrayList<ProductDetailsBean.ListBean> data, Context context, ListView listView) {
        this.mData = data;
        this.mContext = context;
        mListView = listView;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.buy_item_good_detial, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ViewGroup.LayoutParams params = finalConvertView.getLayoutParams();
                params.height = (int) ((mListView.getHeight() - 15) / 7);
                finalConvertView.setLayoutParams(params);
                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        ImageloaderUtils.displayRoundImg(ImageloaderUtils.getImageLoader(), mData.get(position).getImg(), mHolder.mBuyDetialLogo);
        mHolder.mBuyDetialName.setText(mData.get(position).getRsName());
        mHolder.mBuyDetialPrice.setText("¥" + mData.get(position).getOriginPrice());
        String buyDetial = "x" + mData.get(position).getRsQty();
        String rsUnit = mData.get(position).getRsUnit();
        if (!TextUtils.isEmpty(rsUnit)) {
            buyDetial += rsUnit;
        }
        mHolder.mBuyDetialCount.setText(buyDetial);
        return convertView;
    }

    class ViewHolder {
        private ImageView mBuyDetialLogo;
        private TextView mBuyDetialName;
        private TextView mBuyDetialPrice;
        private TextView mBuyDetialCount;

        public ViewHolder(View view) {
            mBuyDetialLogo = (ImageView) view.findViewById(R.id.img_buydetial_logo);
            mBuyDetialName = (TextView) view.findViewById(R.id.tv_buydetial_name);
            mBuyDetialPrice = (TextView) view.findViewById(R.id.tv_buydetial_price);
            mBuyDetialCount = (TextView) view.findViewById(R.id.tv_buydetial_count);
        }
    }
}
